package com.zrgg.futuwen.presenter;

import android.app.Activity;

import com.iceteck.silicompressorr.SiliCompressor;
import com.zrgg.futuwen.MainActivity;
import com.zrgg.futuwen.R;
import com.zrgg.futuwen.Urltool;
import com.zrgg.futuwen.app.ObservableManager;
import com.zrgg.futuwen.base.BaseContract;
import com.zrgg.futuwen.contract.MainContract;
import com.zrgg.futuwen.network.ServiceFactory;
import com.zrgg.futuwen.network.data_model.FaceData;
import com.zrgg.futuwen.network.data_model.FileData;
import com.zrgg.futuwen.network.data_model.MainUrlData;
import com.zrgg.futuwen.network.observable_interface.MyService;
import com.zrgg.futuwen.utils.Logger;

import java.io.File;

import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.RequestBody;
import rx.Observable;
import rx.Subscriber;

/**
 * Created by San on 2017/10/23.
 */

public class MainPresenter extends BaseContract.Presenter<MainContract.View> {

    public MainPresenter(MainContract.View baseView) {
        super(baseView);
    }

    public void getMainUrl() {
        Observable<MainUrlData> uploadoneimage = ServiceFactory.creatService(MyService.class).getUrl();
        new ObservableManager<MainUrlData>().onResponse(uploadoneimage, new Subscriber<MainUrlData>() {
            public void onCompleted() {
            }

            public void onError(Throwable e) {
                if (baseView != null) {
                    baseView.dismissDialog();
                    baseView.showToast(R.string.wangluoyichang);
                }
            }

            public void onNext(MainUrlData fileData) {
                if (baseView != null) {
                    baseView.dismissDialog();
                    if (fileData.isOk()) {

//                        fileData.url = "https://www.baidu.com";

                        // 初始化url
                        Urltool.init(fileData.url);
                        baseView.getMainUrlRet(fileData.url);
                    } else baseView.showToast(fileData.error);
                }
            }
        });
        if (baseView != null) baseView.showDialog();
    }

    private MultipartBody getUpImgData(String imgPath) {
        MultipartBody.Builder builder = new MultipartBody.Builder();
        File file = new File(imgPath);
        //这里为了简单起见，没有判断file的类型
        RequestBody requestBody = RequestBody.create(MediaType.parse("image/png"), file);
        builder.addFormDataPart("file", file.getName(), requestBody);
        builder.setType(MultipartBody.FORM);
        MultipartBody multipartBody = builder.build();
        return multipartBody;
    }

    public void upDataImg(final String imgPathf) {

        new Thread(new Runnable() {

            String imgPath;


            @Override
            public void run() {

                imgPath = imgPathf;


                File file = new File(imgPath);


                if (file.isFile()) {

                    Logger.log(getClass().getSimpleName(), file.length() + ":file.length()");

                    File bitfiledir = MainActivity.context.getExternalFilesDir("bit");

                    imgPath = SiliCompressor.with(MainActivity.context).compress(imgPath, bitfiledir);
                }

                ((Activity) MainActivity.context).runOnUiThread(new Runnable() {
                    @Override
                    public void run() {

                        MultipartBody upImgData = getUpImgData(imgPath);
                        Observable<FileData> uploadoneimage = ServiceFactory.creatFaceService(MyService.class).uploadoneimage(upImgData.parts());
                        new ObservableManager<FileData>().onResponse(uploadoneimage, new Subscriber<FileData>() {
                            public void onCompleted() {
                            }

                            public void onError(Throwable e) {
                                if (baseView != null) {
                                    baseView.dismissDialog();
                                    baseView.showToast(R.string.wangluoyichang);
                                }
                            }

                            public void onNext(FileData fileData) {
                                if (baseView != null) {
                                    baseView.dismissDialog();
                                    if (fileData.isOk()) {
                                        baseView.upDataImgSucc(fileData.url);
                                    } else baseView.showToast(fileData.error);
                                }
                            }
                        });
                        if (baseView != null) baseView.showDialog();
                    }
                });

            }
        }).start();

    }


    public void facerecognition(File file, String userid) {

        RequestBody fileBody = RequestBody.create(MediaType.parse("IMAGE/JPEG"), file);
        MultipartBody.Part filePart = MultipartBody.Part.createFormData("img", file.getName(), fileBody);
        MultipartBody.Part id = MultipartBody.Part.createFormData("user_id", userid);

        Observable<FaceData> uploadoneimage = ServiceFactory.creatFaceService(MyService.class).facerecognition(id, filePart);

        new ObservableManager<FaceData>().onResponse(uploadoneimage, new Subscriber<FaceData>() {
            public void onCompleted() {
            }

            public void onError(Throwable e) {
                if (baseView != null) {
                    baseView.dismissDialog();
                    baseView.showToast(R.string.wangluoyichang);
                }
            }

            public void onNext(FaceData fileData) {
                if (baseView != null) {
                    baseView.dismissDialog();
                    if (fileData.isOk()) {
                        baseView.facerecognition(fileData);
                    } else baseView.showToast(fileData.error);
                }
            }
        });
        if (baseView != null) baseView.showDialog();
    }

    private MultipartBody getUpSoundData(String fileStr) {
        MultipartBody.Builder builder = new MultipartBody.Builder();
        File file = new File(fileStr);
        //这里为了简单起见，没有判断file的类型
        RequestBody requestBody = RequestBody.create(MediaType.parse("audio/mp3"), file);
        builder.addFormDataPart("file", file.getName(), requestBody);
        builder.setType(MultipartBody.FORM);
        MultipartBody multipartBody = builder.build();
        return multipartBody;
    }

    public void upDataSound(String fileStr) {
        MultipartBody upImgData = getUpSoundData(fileStr);
        Observable<FileData> uploadoneimage = ServiceFactory.creatService(MyService.class).uploadonesound(upImgData.parts());
        new ObservableManager<FileData>().onResponse(uploadoneimage, new Subscriber<FileData>() {
            public void onCompleted() {
            }

            public void onError(Throwable e) {
                if (baseView != null) {
                    baseView.dismissDialog();
                    baseView.showToast(R.string.wangluoyichang);
                }
                if (Logger.isDebug && e != null) e.printStackTrace();
            }

            public void onNext(FileData fileData) {
                if (baseView != null) {
                    baseView.dismissDialog();
                    if (fileData.isOk()) {
                        baseView.upDataSoundSucc(fileData.url);
                    } else baseView.showToast(fileData.error);
                }
            }
        });
        if (baseView != null) baseView.showDialog();
    }

//    private MultipartBody filesToMultipartBody() {
//        MultipartBody.Builder builder = new MultipartBody.Builder();
//        for (int i = 1; i <= modeList.size(); i++) {
//            ImgMode imgMode = modeList.get(i - 1);
//            File file = new File(imgMode.getImgUrl());
//            //这里为了简单起见，没有判断file的类型
//            RequestBody requestBody = RequestBody.create(MediaType.parse("image/png"), file);
//            //builder.addFormDataPart("file", file.getName(), requestBody);
//            builder.addFormDataPart("file" + i, file.getName(), requestBody);}
//        builder.addFormDataPart("token", UserConfig.getToken());
//        builder.addFormDataPart("userType", "2");
//        builder.setType(MultipartBody.FORM);
//        MultipartBody multipartBody = builder.build();
//        return multipartBody;
//    }
}